import java.net.*;
import java.io.*;


public class socketClient {
	public static void main (String[] args) {
		//define o server
		String host = "localhost";
		
		//define o porto de destino
		int port = 2222;

		StringBuffer instr = new StringBuffer();
		String TimeStamp;
	
		System.out.println("Socket iniciado");

		try {

			InetAddress address = InetAddress.getByName(host);
			Socket connection = new Socket(address,port);

			BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
			
			OutputStreamWriter osw = new OutputStreamWriter(bos,"US-ASCII");

			TimeStamp = new java.util.Date().toString();
	//		String process = "Calling the Socket Server on " + host + " port " + port + " at " + TimeStamp + (char) 13;

	//		osw.write(process);
	//		osw.flush();
			String processs = "Priest FTW!---- For the ALLIANCE!" + (char) '&' ;
			osw.write(processs);
			osw.flush();
			//connection.close();
			BufferedInputStream bis = new BufferedInputStream(connection.
          getInputStream());
			
			InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

		      /**Read the socket's InputStream and append to a StringBuffer */
		      int c;
		      while ( (c = isr.read()) != '&')
		        instr.append( (char) c);
		
		      /** Close the socket connection. */
		      connection.close();
		      System.out.println(instr);
 
		}
		catch (IOException f){
			System.out.println("IOException: " + f);
		}
		catch (Exception g){
			System.out.println("IOException: " + g);
		
		}
	}
}
